define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Seckill, $window, $timeout, $ionicSideMenuDelegate, GoPage, $interval, $ionicSlideBoxDelegate) {
		$scope.isLoad = false; //可以加载
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.selectIndex = 0;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				$scope.lan = 20;
			}
			$scope.all = $window.innerHeight;
			$scope.init_data();
		}, false);
		
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
		//加载数据
		$scope.index = 1;
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		$scope.set_timer = function(now_time) {
			$scope.timer =  $interval(function() {
				if($scope.datas){
				for(var i = 0; i < $scope.datas.act.length; i++) {
					if($scope.datas.act[i].now_time != undefined)
						$scope.datas.act[i].now_time = $scope.datas.act[i].now_time - 1;
					else
						$scope.datas.act[i].now_time = $scope.datas.act[i].end_time - now_time;
					$scope.datas.act[i].formate_time = $scope.formate_time($scope.datas.act[i].now_time);
				}	
				}
				if($scope.doing){
				for(var i = 0; i < $scope.doing.act.length; i++) {
					if($scope.doing.act[i].now_time != undefined)
						$scope.doing.act[i].now_time = $scope.doing.act[i].now_time - 1;
					else
						$scope.doing.act[i].now_time = $scope.doing.act[i].end_time - now_time;
					$scope.doing.act[i].formate_time = $scope.formate_time($scope.doing.act[i].now_time);
				}	
				}
				if($scope.will){
				for(var i = 0; i < $scope.will.act.length; i++) {
					if($scope.will.act[i].now_time != undefined)
						$scope.will.act[i].now_time = $scope.will.act[i].now_time - 1;
					else
						$scope.will.act[i].now_time = $scope.will.act[i].start_time - now_time;
					$scope.will.act[i].formate_time = $scope.formate_time($scope.will.act[i].now_time);
				}	
				}
				
			}, 1000)
		}
		$scope.init_time = function(data, now_time) {
			for(var i = 0; i < data.act.length; i++) {
				if(data.act[i].now_time != undefined)
					data.act[i].now_time = data.act[i].now_time - 1;
				else {
					if(data.act_status==1){
						data.act[i].now_time = data.act[i].start_time - now_time;
					}else{
						data.act[i].now_time = data.act[i].end_time - now_time;
					}
				}
				data.act[i].formate_time = $scope.formate_time(data.act[i].now_time);
			}

		}
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
//				console.log(1);
			} else {
				return '0天0小时0分0秒';
			}
		}
		$scope.init_data = function() {

			//获取列表数据
			Seckill.seckill_list(1, '', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.set_timer(now_time);
					$scope.datas = data;
				} else {
					$scope.datas = new Object();

				}
				$scope.input.nPage = 1;
			});
			Seckill.seckill_list(1, 'started', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.doing = data;
				} else {
					$scope.doing = new Object();

				}
				$scope.input.doing_nPage = 1;
			});
			Seckill.seckill_list(1, 'will', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.will = data;
				} else {
					$scope.will = new Object();

				}
				$scope.input.will_nPage = 1;

			});
			Seckill.seckill_list(1, 'ended', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.ended = data;
				} else {
					$scope.ended = new Object();
					$scope.input.ended_nPage = 1;
				}
			});
			
			//轮播图
			Seckill.getBannerList(function(data){
				$scope.banners = data.result.associate;
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
				$scope.getAdvsBox();
			})
		}
		
		//获取轮播图的高度。获取到的高度一般是最高的那张图片的高度，如果接口比较慢，得到的很可能是最矮的那张
		$scope.getAdvsBox = function(){
			$timeout(function(){
				$scope.advsBox = document.getElementById("advsBox").clientHeight;
				if($scope.banners==null||$scope.banners.length==0||$scope.advsBox){//没有轮播图或已经获取到了高度，则取消定时器
					$interval.cancel($scope.getAdvsBox);
				}
				$scope.myObj = {
					"height": $scope.all - 44*2 - $scope.advsBox - $scope.lan + "px"
				}
//				console.log($scope.myObj)
			},500)
		}
		
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			if($scope.selectIndex==3){
				Seckill.seckill_list(1,'', function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.datas = data;
				} else 
					$scope.datas = new Object();
					$scope.input.nPage = 1;
				
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}else if($scope.selectIndex==0){
				Seckill.seckill_list(1,'started',function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.doing = data;
					//					$scope.set_timer()
				} else 
					$scope.doing = new Object();
					$scope.input.doing_nPage = 1;
				
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}else if($scope.selectIndex==1){
				Seckill.seckill_list(1, 'will',function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.will = data;
					//					$scope.set_timer()
				} else {
					$scope.will = new Object();
					$scope.input.will_nPage = 1;
				}
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}else{
				Seckill.seckill_list(1,'ended' ,function(data, now_time) {
				if(data != null) {
					$scope.init_time(data, now_time);
					$scope.ended = data;
				} else 
					$scope.ended = new Object();
					$scope.input.ended_nPage = 1;
				
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}
			

		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.selectIndex==3){
				if($scope.datas.act==undefined){
					$scope.input.nPage = 0;
				}else if($scope.datas.page.count <= $scope.datas.act.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.datas.page.count<$scope.input.nPage*20){
					$scope.input.nPage -=1;
					$scope.count_f = $scope.input.nPage *20;
						}else{
						$scope.count_f = null;
					}
				}
				Seckill.seckill_list($scope.input.nPage + 1,'', function(data, now_time) {
				if(data != null) {
					if($scope.datas.act != undefined) {
						if($scope.count_f!=null)
						$scope.datas.act.splice($scope.count_f,$scope.datas.page.count-$scope.count_f);
						$scope.init_time(data, now_time);
						$scope.datas.act = $scope.datas.act.concat(data.act);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}

			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}else if($scope.selectIndex==0){
				if($scope.doing.act==undefined){
					$scope.input.doing_nPage = 0;
				}else if($scope.doing.page.count <= $scope.doing.act.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.doing.page.count<$scope.input.doing_nPage*20){
					$scope.input.doing_nPage -=1;
					$scope.count_s = $scope.input.doing_nPage *20;
						}else{
						$scope.count_s = null;
					}
				}
				Seckill.seckill_list($scope.input.doing_nPage + 1,'started', function(data, now_time) {
				if(data != null) {
					if($scope.doing.act != undefined) {
						if($scope.count_s!=null)
						$scope.doing.act.splice($scope.count_s,$scope.doing.page.count-$scope.count_s);
						$scope.init_time(data, now_time);
						$scope.doing.act = $scope.doing.act.concat(data.act);
						$scope.input.doing_nPage += 1;
					} else {
						$scope.doing = data;
						$scope.input.doing_nPage = 1;
					}
				}

			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}else if($scope.selectIndex==1){
				if($scope.will.act==undefined){
					$scope.input.will_nPage = 0;
				}else if($scope.will.page.count <= $scope.will.act.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.will.page.count<$scope.input.will_nPage*20){
					$scope.input.will_nPage -=1;
					$scope.count_t = $scope.input.will_nPage *20;
						}else{
						$scope.count_t = null;
					}
				}
				Seckill.seckill_list($scope.input.will_nPage + 1,'will', function(data, now_time) {
				if(data != null) {
					if($scope.will.act != undefined) {
						if($scope.count_t!=null)
						$scope.will.act.splice($scope.count_t,$scope.will.page.count-$scope.count_t);
						$scope.init_time(data, now_time);
						$scope.will.act = $scope.will.act.concat(data.act);
						$scope.input.will_nPage += 1;
					} else {
						$scope.will = data;
						$scope.input.will_nPage = 1;
					}
				}

			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}else{
				if($scope.ended.act==undefined){
					$scope.input.ended_nPage = 0;
				}else if($scope.ended.page.count <= $scope.ended.act.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.ended.page.count<$scope.input.ended_nPage*20){
					$scope.input.ended_nPage -=1;
					$scope.count_fo = $scope.input.ended_nPage *20;
						}else{
						$scope.count_fo = null;
					}
				}
				Seckill.seckill_list($scope.input.ended_nPage + 1,'ended', function(data, now_time) {
				if(data != null) {
					if($scope.datas.act != undefined) {
						if($scope.count_fo!=null)
						$scope.ended.act.splice($scope.count_fo,$scope.ended.page.count-$scope.count_fo);
						$scope.init_time(data, now_time);
						$scope.ended.act = $scope.ended.act.concat(data.act);
						$scope.input.ended_nPage += 1;
					} else {
						$scope.ended = data;
						$scope.input.ended_nPage = 1;
					}
				}

			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
			}
			
		};

		$scope.onshow_detail = function(act_id,type) {
//			console.log(act_id);
			GoPage.go_states("seckilldetail", {
				'act_id': act_id,
				'type':type
			});
		};
		$scope.go_store = function(store_id){
			GoPage.go_states("first", {
				'store_id': store_id
			});
		}
	}

	controller.$inject = ['$scope', 'Seckill', '$window', '$timeout', '$ionicSideMenuDelegate', 'GoPage', '$interval', '$ionicSlideBoxDelegate'];

	return app.registerController("seckillctrl",controller);
});